<template>
    <div class="components_edit_modal">
        <el-dialog :title="edited?'专业修改':'专业添加'" :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <el-form :model="professionParams"   :rules="rules" ref="professionParams"label-position="right">

                <el-form-item label="学校"  prop="schoolId">
                    <el-select v-model="professionParams.schoolId" placeholder="请选择学校"  value-key="value"
                            @change="changeSchool">
                    <el-option
                        v-for="item in schoolList" :key="item.id"
                        :label="item.text"
                        :value="item.id">
                    </el-option>
                    </el-select>
                 </el-form-item>

                <el-form-item label="学院" prop="secondSchoolId">
                    <el-select v-model="professionParams.secondSchoolId" placeholder="请选择学院" value-key="value">
                        <el-option
                             v-for="item in secondSchoolList" :key="item.id"
                            :label="item.text"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="专业名称"  prop="professionName">
                    <el-input v-model="professionParams.professionName"></el-input>
                </el-form-item>

                <el-form-item label="专业代码" prop="professionNo">
                    <el-input v-model="professionParams.professionNo"></el-input>
                </el-form-item>


                <el-form-item label="专业性质"  prop="nature">
                    <el-select v-model="professionParams.nature" placeholder="请选择" value-key="value">
                        <el-option
                            v-for="item in natureList" :key="item.id"
                            :label="item.name"
                            :value="item.value">
                        </el-option>
                        </el-select>
                </el-form-item>
                <!--
                <el-form-item label="专业组" prop="professionGroup">
                    <el-select v-model="professionParams.professionGroup" placeholder="请选择" value-key="value">
                        <el-option
                             v-for="item in professionGroupList" :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                -->

                <el-form-item label="学制"  prop="lengthOfSchooling">
                    <el-select v-model="professionParams.lengthOfSchooling" placeholder="请选择" value-key="value">
                        <el-option
                            v-for="item in lengthOfSchoolingList" :key="item.value"
                            :label="item.name"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="校区主管" prop="schoolLeader">
                    <el-select v-model="professionParams.schoolLeader" filterable placeholder="请选择" value-key="value">
                        <el-option
                             v-for="item in schoolLeaderList" :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <!-- 
                <el-form-item label="专业负责人"  prop="professionLeader">
                    <el-select v-model="professionParams.professionLeader" filterable placeholder="请选择" >
                        <el-option
                            v-for="item in professionLeaderList" :key="item.id"
                            :label="item.name"
                            :value="item.id">
                        </el-option>
                        </el-select>
                </el-form-item>
                -->

                <!-- <el-form-item label="培养方案" >
                    <pl-upload :initionData="professionParams.headPortraits"  :imageOnly="false" extensions="xls,xlsx" :multiFile="false" @reloadFiles="reloadFiles" ></pl-upload>
                </el-form-item> -->


            </el-form>

            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="small" @click="addProfession('professionParams')">确 定</el-button>
                <el-button @click="state = false"  size="small">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import {enums} from '@/constant'
    import PlUpload from "../../../common/PlUpload"
    export default {
        data() {
            return {
                 state: false,
                 edited:false,
                 rules: {
                    schoolId: [
                        { required: true, message: '请选择学校', trigger: 'change', type: 'number' }
                    ],
                    secondSchoolId: [
                         { required: true, message: '请选择学院', trigger: 'change', type: 'number' }
                    ],
                    professionName: [
                        { required: true, message: '专业名称不能为空', trigger: 'blur'},
                    ],
                    professionNo: [
                        { required: true, message: '专业代码不能为空', trigger: 'blur'}
                    ],
                    nature: [
                        { required: true, message: '请选择专业性质', trigger: 'change'},
                    ],
                    professionGroup: [
                        { required: true, message: '请选择专业组', trigger: 'change' },
                    ],
                    lengthOfSchooling: [
                        { required: true, message: '请选择学制', trigger: 'change'},
                    ],
                    schoolLeader: [
                        { required: true, message: '请选择校区主管', trigger: 'change' ,type: 'number'},
                    ],
                    professionLeader: [
                        { required: true, message: '请选择专业负责人', trigger: 'change' ,type: 'number'},
                    ],
                 },
                professionParams: {
                    id: null,
                    schoolId:null,
                    schoolName:null,
                    secondSchoolId:null,
                    secondSchoolName:null,
                    professionName:null,
                    professionNo:null,
                    nature:null,
                    professionGroup:null,
                    lengthOfSchooling:null,
                    schoolLeader:null,
                    schoolLeaderName:null,
                    professionLeader:null,
                    professionLeaderName:null,
                    headPortraits:[]
                },
                // 学校列表
                schoolList:[],
                // 二级院校列表
                secondSchoolList:[],
                 // 专业性质枚举
                // natureList: enums.SCHOOL_TYPE,
                natureList:[],
                // 专业组
                professionGroupList:[],
                //学制
                lengthOfSchoolingList:[],
                //校区主管
                schoolLeaderList:[],
                //负责人
                professionLeaderList:[],
                professionId:null
            }
        },
        created() {
        },
        mounted() {},
        methods: {
            // 打开模态框
            showDialog(schoolId,secondSchoolId){
                // console.log(schoolId+"-------->"+secondSchoolId);
                let self = this;
                if(this.$refs['professionParams']!==undefined){
                    this.$refs['professionParams'].resetFields();
                }
                // 获取学校数据
                this.getSchoolList();
                //获取专业组
                this.getProfessionGroupList();
                //获取学制
                this.getLengthOfSchoolingList();
                //获取负责人
                // this.getProfessionLeaderList();
                this.getProfessionType();
                if(schoolId != null){
                    this.getSecondSchoolList(schoolId,(response)=>{
                        self.secondSchoolList = response;
                        self.getSchoolLeaderList(schoolId);
                        self.state = true;
                        self.edited=false;
                        self.professionParams.schoolId = schoolId;
                        self.professionParams.secondSchoolId = secondSchoolId;
                    });
                }else{
                    self.state = true;
                    self.edited=false;
                }
            },
            editProfession(id,schoolId){
                let self = this;
                this.state = true;
                this.edited=true;
                // 获取学校数据
                this.getSchoolList();
                //获取专业组
                this.getProfessionGroupList();
                //获取学制
                this.getLengthOfSchoolingList();
                //获取负责人
                // this.getProfessionLeaderList();
                this.getProfessionType();
                if(this.$refs['professionParams']!==undefined){
                    this.$refs['professionParams'].resetFields();
                }
                this.professionId = id;
                this.getSecondSchoolList(schoolId,(response)=>{
                        self.secondSchoolList = response;
                        self.getSchoolLeaderList(schoolId);
                        self.getProfessionDetail(id);
                });
            },
            
            //获取专业详情
            getProfessionDetail(id){
                let vm = this;
                vm.axios.get(api.professionManager.detail+id).then((response) => {
                    if (response.data.success) {
                        let school = response.data.data;
                        let professionParams = {
                            schoolId:school.schoolId,
                            schoolName:school.schoolName,
                            secondSchoolId:school.secondSchoolId,
                            secondSchoolName:school.secondSchoolName,
                            professionName:school.professionName,
                            professionNo:school.professionNo,
                            nature:school.nature,
                            professionGroup:school.professionGroup,
                            lengthOfSchooling:school.lengthOfSchooling,
                            schoolLeader:school.schoolLeader,
                            schoolLeaderName:school.schoolLeaderName,
                            professionLeader:school.professionLeader,
                            professionLeaderName:school.professionLeaderName,
                            headPortraits:[]
                        };
                        this.professionParams = professionParams;
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                    })
            },
            // 新建专业
            addProfession(professionParams) {
                let vm = this;
                this.$refs['professionParams'].validate((valid) => {
                    if (valid) {
                        let url = api.professionManager.insert;
                       // let professionParamsN = this.getPassParams();
                       // 组装数据
                        vm.professionParams.schoolName = _.find(vm.schoolList, {id: vm.professionParams.schoolId}).text;
                        vm.professionParams.secondSchoolName = _.find(vm.secondSchoolList, {id: vm.professionParams.secondSchoolId}).text;
                        // vm.professionParams.professionLeader = _.find(vm.professionGroupList, {id: vm.professionParams.professionGroup}).leaderId;
                        // console.log(vm.professionParams);
                        // return;
                        if(vm.edited){
                            url = api.professionManager.update;
                            vm.professionParams.id = vm.professionId;
                            this.axios.put(url, vm.professionParams)
                            .then((response) => {
                            if (response.data.success) {
                                vm.$message({
                                    type: 'success',
                                    message: response.data.msg,
                                });
                                vm.state=false;
                                vm.$parent.$refs.professionTable.$emit('vuetable:refresh');
                            } else {
                                vm.$message.error(response.data.msg);
                            }
                        })
                        }else{
                            // console.log(JSON.stringify(professionParamsN))
                        this.axios.post(url, vm.professionParams)
                        .then((response) => {
                            if (response.data.success) {
                                vm.$message({
                                    type: 'success',
                                    message: response.data.msg,
                                });
                                vm.state=false;
                                vm.$parent.$refs.professionTable.$emit('vuetable:refresh');
                            } else {
                                vm.$message.error(response.data.msg);
                            }
                        })
                        }
                    } else {
                        return false;
                    }

                });

            },
            // 获取学校数据
            getSchoolList() {
                let vm = this;
                vm.axios.get(api.getSchoolList).then((response) => {
                    if (response.data.success) {
                        vm.schoolList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
             
            //学校监听
            changeSchool(schoolId){
                this.professionParams.schoolLeaderName=null;
                this.professionParams.schoolLeader=null;
                
                this.professionParams.secondSchoolId= null;
                this.professionParams.secondSchoolName = null;
                
                this.secondSchoolList = [];
                this.schoolLeaderList = [];
                this.getSecondSchoolList(schoolId,(response)=>{
                    this.secondSchoolList = response;
                });
                this.getSchoolLeaderList(schoolId);
            },
            // 获取二级院系数据
            getSecondSchoolList(schoolId,success) {
                let vm = this;
                vm.axios.get(api.getSecondSchoolList,{
                    params: {
                        schoolId: schoolId
                    }
                }).then((response) => {
                    if (response.data.success) {
                        success(JSON.parse(JSON.stringify(response.data.data)));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
            },
            getProfessionGroupList(){
                let vm = this;
                /* 
                this.getDictsQueryByType("profession_group",(result)=>{
                    
                        
                }); 
                */
                vm.axios.get(api.professionGroup.queryAllList).then((response) => {
                    if (response.data.success) {
                        if(_.isEmpty(response.data.data)){
                            return;
                        }
                        vm.professionGroupList = JSON.parse(JSON.stringify(response.data.data));
                        vm.professionGroupList.forEach((item)=>{
                            item.id = item.id + "";
                        })
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                })
                
                
            },
            //获取学制
            getLengthOfSchoolingList(){
                let self = this;
                this.getDictsQueryByType("PROFESSION_EDUSYSTEM",(result)=>{
                    self.lengthOfSchoolingList = JSON.parse(JSON.stringify(result));
                });
            },
            //获取专业性质
            getProfessionType(){
                let self = this;
                this.getDictsQueryByType("profession_type_new",(result)=>{
                    self.natureList = JSON.parse(JSON.stringify(result));
                });
            },

            //获取校区主管
            getSchoolLeaderList(id){
                let vm = this;
                vm.axios.get(api.getSchoolLeadersBySchoolId, {
                        params: {
                            schoolId:id
                        }
                    }
                ).then((response) => {
                    if (response.data.success) {
                        // console.log(JSON.stringify(response.data.data));
                        vm.schoolLeaderList = JSON.parse(JSON.stringify(response.data.data));
                        if(response.data.data.length == 1){
                            vm.professionParams.schoolLeader = response.data.data[0].id;
                        }
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                });
            },
            //获取负责人
            getProfessionLeaderList(){
                let vm = this;
                vm.axios.get(api.queryAllUserList
                ).then((response) => {
                    if (response.data.success) {
                        vm.professionLeaderList = JSON.parse(JSON.stringify(response.data.data));
                        vm.schoolLeaderList = JSON.parse(JSON.stringify(response.data.data));
                    } else {
                        vm.$message.error(response.data.msg);
                    }
                });
            },
            getDictsQueryByType(type,success){
                let vm = this;
                vm.axios.get(api.dictsTypesSubListUrl,{
                    params: {
                        type: type
                    }
                }).then((response) => {
                    if (response.data.success) {
                        success(response.data.data);

                    } else {
                        vm.$message.error(response.data.msg);
                    }
                });
            },
            //补全 name
            getPassParams(){
                let professionParams = this.professionParams;

                let schoolList = this.schoolList;
                let secondSchoolList = this.secondSchoolList;
                let schoolLeaderList = this.schoolLeaderList;
                let professionLeaderList = this.professionLeaderList;

                for(let i=0;i<schoolList.length;i++){
                    if(schoolList[i].id == professionParams.schoolId){
                        professionParams.schoolName = schoolList[i].text;
                    }
                }
                for(let i=0;i<secondSchoolList.length;i++){
                    if(secondSchoolList[i].id == professionParams.secondSchoolId){
                        professionParams.secondSchoolName = secondSchoolList[i].text;
                    }
                }
                for(let i=0;i<schoolLeaderList.length;i++){
                    if(schoolLeaderList[i].id == professionParams.schoolLeader){
                        professionParams.schoolLeaderName = schoolLeaderList[i].name;
                    }
                }
                for(let i=0;i<professionLeaderList.length;i++){
                    if(professionLeaderList[i].id == professionParams.professionLeader){
                        professionParams.professionLeaderName = professionLeaderList[i].name;
                    }
                }
                return professionParams;
            },
            clear(){
                this.professionParams={
                    schoolId:null,
                    schoolName:null,
                    secondSchoolId:null,
                    secondSchoolName:null,
                    professionName:null,
                    professionNo:null,
                    nature:null,
                    professionGroup:null,
                    lengthOfSchooling:null,
                    schoolLeader:null,
                    schoolLeaderName:null,
                    professionLeader:null,
                    professionLeaderName:null,
                    headPortraits:null
                }
            },
            reloadFiles: function (files) {
                this.professionParams.headPortraits  = files;
            }
        },
        components: {
            PlUpload
        },

    };
</script>
<style scoped>

</style>